package bean.dao;

import hbt.HibernateUtil;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import bean.Alumno;
import bean.Profesor;

public class HibernateCursosDAO {
	
	private static HibernateCursosDAO _instance;
	private static SessionFactory _sf;
	
	private HibernateCursosDAO(){}
	
	public static HibernateCursosDAO getInstance()
	{
		if (_instance==null)
		{
			_instance = new HibernateCursosDAO();
			_sf= HibernateUtil.getSessionFactory();
		}
		return _instance;
			
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> alummnosPorCurso(){
		Session session = _sf.openSession();
		System.out.println("BF LISTA");
		List<Object[]> lista = session.createQuery(
				"select cur.id, cur.materia.descripcion, cur.alumnos.size from Curso cur")
				.list();

		session.close();
		System.out.println("ELEMENTOS "+lista.size());
		return lista;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> profesoresMultiplesMat(){
		Session session = _sf.openSession();
		List<Object[]> lista = session.createQuery(
				"select a.Nombre, a.ID, a.materias.size from Profesor a where a.materias.size > 1")
				.list();

		session.close();
		return lista;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Alumno> obtenerAlumnos(){
		Session session = _sf.openSession();
		List<Alumno> lista = session.createQuery("from Alumno").list();

		session.close();
		return lista;
	}
	
	
	@SuppressWarnings("unchecked")
	public Profesor getProfesor(String nombre){
		Session session = _sf.openSession();
		Profesor p = (Profesor)session.createQuery("from Profesor a where a.Nombre =?").setString(0, nombre).uniqueResult();

		session.close();
		return p;
	}
	

}
